/**
 * 抽象工厂模式
 *      用抽象类创建的对象作为父类来创建一些子类
 */
/**
 * 抽象工厂方法
 * 有三个产品簇抽象类Car、Bus、Truck
 */
var VehicleFactory = function(subType, superType) {
    if (typeof VehicleFactory[superType] === 'function') {
        // 缓存类
        function F() {};
        // 继承父类属性和方法
        F.prototype = new VehicleFactory[superType]();
        // 将子类constructor指向子类
        subType.constructor = subType;
        // 子类原型继承父类
        subType.prototype = new F();
        //？？有问题，对constructor先赋值，再声明prototype是F的实例，是否会影响constructor => 并不会
    } else {
        throw new Error('未创建该抽象类');
    }
}

VehicleFactory.Car = function() {
    this.type = 'car';
};

VehicleFactory.Car.prototype = {
    getPrice: function() {
        return new Error('抽象方法不能调用');
    },
    getSpeed: function() {
        return new Error('抽象方法不能调用');
    }
};

// 公交车抽象类
VehicleFactory.Bus = function() {
    this.type = 'bus';
}

VehicleFactory.Bus.prototype = {
    getPrice: function() {
        return new Error('抽象方法不能调用');
    },
    getPassengerNum: function() {
        return new Error('抽象方法不能调用');
    }
};

VehicleFactory.Trunk = function() {
    this.type = 'trunk';
}
VehicleFactory.Trunk.prototype = {
    getPrice: function() {
        return new Error('抽象方法不能调用');
    },
    getTrainload: function() {
        return new Error('抽象方法不能调用');
    }
};

// BWM汽车子类
var BMW = function(price, speed) {
    this.price = price;
    this.speed = speed;
}
// 抽象工厂实现对Car抽象类的继承
VehicleFactory(BMW, 'Car');
BMW.prototype.getPrice = function() {
    return this.price;
};
BMW.prototype.getSpeed = function() {
    return this.speed;
}

var Lamborghini = function(price, speed) {
    this.price = price;
    this.speed = speed;
}
VehicleFactory(Lamborghini, 'Car');
Lamborghini.prototype.getPrice = function() {
    return this.price;
};
Lamborghini.prototype.getSpeed = function() {
    return this.speed;
}

var bmwCar = new BMW(100000, 800);
var lamborghiniCar = new Lamborghini(1000000, 10000);
console.log(bmwCar.getPrice());
console.log(lamborghiniCar.getSpeed());
